"""
1. 递归函数： 自己调用自己
"""

# 1+2+3+4+5++...+n

# 没有学习函数之前写死
# total = 0
# for i in range(1, 60 + 1):
#     total += i
# print(total)


# 使用函数
# def get_sum(n):
#     total = 0
#     for i in range(1, n+1):
#         total += i
#     return total
#
# print(get_sum(60))


# 递归函数实现  要素：有递归出口
# 1+2+3+4+5+6   f(6)
#               6 + f(5)
#               6 + 5 + f(4)
#               6 + 5 + 4 + f(3)
#               6 + 5 + 4 + 3 + f(2)
#               6 + 5 + 4 + 3 + 2 + f(1)

# import sys
#
# print(sys.getrecursionlimit(), "默认最大递归深度")
# sys.setrecursionlimit(2000000)
# print(sys.getrecursionlimit(), " 更新最大递归深度")
#
# import time
#
#
# def get_sum(n):
#     # 递归出口
#     if n == 1:
#         return 1
#     else:
#         return n + get_sum(n - 1)
#
#
# start = time.time()
# print(f"开始前时间", start)
# print(get_sum(1999999))
# end = time.time()
# print(f"执行递归消耗时间", end - start)
#
# start = time.time()
# print(f"开始前时间", start)
# total = 0
# for i in range(1, 2000000):
#     total += i
# # print(total)
# end = time.time()
# print(f"执行普通计算消耗时间", end - start)


# def get_mul(n):
#     if n == 1:
#         return 1
#     else:
#         return n * get_mul(n - 1)
#
# print(get_mul(5))


# def fun(a, b):
#     c = a + b
#     return c
#
#
# print(fun(10, 20))
# print(fun(10, 20))
# print(fun(10, 20))


# fun = lambda a, b: a + b
# # print(type(fun))
# print(fun(10, 20))


# fun = lambda: print("醒醒啦")
# print(type(fun))
# fun()

# l = [1, 5, 3, 2, 6, 4, 9]
# l.sort()
# print(l)


# l = [{"id": 101, "name": "a"}, {"id": 102, "name": "b"}, {"id": 104, "name": "c"}, {"id": 103, "name": "d"}]
#
# # 字典不能直接比较
# # l.sort()
#
# # f = lambda x: x["id"]
# # l.sort(key=f)
#
# l.sort(key=lambda x: x["id"])
#
#
# # def compare(x):
# #     return x["id"]
# #
# # l.sort(key=compare)
#
# print(l)


# 比较两个字典的大小
# l = [{"id": 101, "name": "a"}, {"id": 102, "name": "b"}, {"id": 104, "name": "c"}, {"id": 103, "name": "d"}]
#
# def sort(datas, key):
#     # print(datas, type(datas))
#     for e in datas:
#         print(key(e))
#     # print(key, type(key))
#
# sort(l, lambda x: x["id"])


# l = [{"id": 101, "name": "a"}, {"id": 102, "name": "b"}, {"id": 104, "name": "c"}, {"id": 103, "name": "d"}]
# # 把列表中的每一个字典 都作为实参执行lambda  返回字典对应的id  比较时比较的就是id
# print(max(l, key=lambda x: x["id"]))
#
#
# def my_max(datas, key):
#     max_id = None
#     max_object = None
#     for e in datas:
#         # print(key(e))
#         if max_id is None:
#             max_id = key(e)
#             max_object = e
#
#         else:
#             if max_id < key(e):
#                 max_id = key(e)
#                 max_object = e
#
#     return max_object
#     # print(key, type(key))
#
#
# print(my_max(l, key=lambda x: x["id"]))
